<?php
    define('WP_USE_THEMES', false); 
    
    require('./wp-blog-header.php');
    require('./DBHandler.php');

    $dbHandler = new DBHandler;

    $courseId = '';
    $assignmentId = '';
    
    if ($_GET)
    {
        $courseId = $_GET['courseid'];
        $assignmentId = $_GET['assignmentid'];
        $op = $_GET['op'];

        if ($op == 'delete')
        {
            $dbHandler->DeleteAssignment($assignmentId);
            
            header( 'Location: ./ManageAssignments.php?courseid=' . $courseId );
        }
        else if ($op == 'edit')
        {
            $assignmentPost = $dbHandler->GetAssignment($assignmentId);
        }
    }
    else if ($_POST)
    {
        $authorId = 6; // get_current_user_id();
        $courseId = $_POST['courseid'];
        $assignmentId = $_POST['assignmentid'];
        $title = $_POST['txtTitle'];
        $description = $_POST['txtDesc'];
        $op = $_POST['op'];

        if ($op == 'create')
        {
            // create assignment post
            $assignmentId = $dbHandler->CreateAssignment($authorId, $courseId, $title, $description);
            
            // prepare paths of images and jar files
            $imageFileName = $authorId . '_' . $courseId . '_' . $assignmentId . '.png'; // only .png supported now
            $jarFileName = $authorId . '_' . $courseId . '_' . $assignmentId . '.jar';

            // upload the files and get their destination paths
            $destFilePaths = $dbHandler->UploadFiles("filAssignment", array($imageFileName, $jarFileName));

            header( 'Location: ./ManageAssignments.php?courseid=' . $courseId );
        }
        else if ($op == 'edit')
        {
            // update assignment post
            $dbHandler->UpdateAssignment($assignmentId, $authorId, $courseId, $title, $description);

            // prepare paths of images and jar files
            $imageFileName = $authorId . '_' . $courseId . '_' . $assignmentId . '.png'; // only .png supported now
            $jarFileName = $authorId . '_' . $courseId . '_' . $assignmentId . '.jar';

            // upload the files and get their destination paths
            $destFilePaths = $dbHandler->UploadFiles("filAssignment", array($imageFileName, $jarFileName));

            header( 'Location: ./ManageAssignments.php?courseid=' . $courseId );
        }
    }
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h2>Manage Assignment</h2>
        <a href="manageassignments.php?courseid=<?php echo $courseId?>">Back to Manage Assignments</a>
        <br/><br/>
        <form action="manageassignment.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="assignmentid" value="<?php echo $assignmentId ?>">
            <input type="hidden" name="courseid" value="<?php echo $courseId?>">
            <input type="hidden" name="op" value="<?php echo $op?>">
            <table>
                <tr>
                    <td>Title</td>
                    <td><input name="txtTitle" type="text" value="<?php if ($assignmentPost) { echo $assignmentPost->post_title; } ?>"></td>
                </tr>
                <tr>
                    <td>Description</td>
                    <td>
                        <textarea name="txtDesc" rows="5" cols="80"><?php if ($assignmentPost) { echo $assignmentPost->post_content; } ?></textarea>
                    </td>
                </tr>
                <tr>
                    <td>Image or Screenshot<br/>Supported format is .PNG.</td>
                    <td><input type="file" name="filAssignment[]"></td>
                </tr>
                <tr>
                    <td>Sample JAR file</td>
                    <td><input type="file" name="filAssignment[]"></td>
                </tr>
                <tr>
                    <td><input type="submit"></td>
                    <td><input type="reset"></td>
                </tr>
            </table>
        </form>
    </body>
</html>
